package com.lagou.util;

import com.alibaba.fastjson.JSON;

import java.io.IOException;

/**
 * json序列化类
 */
public class JSONSerializer implements Serializer {
    public byte[] serialize(Object object) throws IOException {
        return JSON.toJSONBytes(object);
    }

    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws IOException {
        String s = new String(bytes, "UTF-8");
        if (s.startsWith("{")){
            return JSON.parseObject(s, clazz);
        }
        return JSON.parseObject(s.indexOf("{") > 0 ? s.substring(s.lastIndexOf("{")) : s, clazz);
    }
}
